#pragma once
#include "cartridge.h"

#include <iostream>
#include <iomanip>
#include <ctime>
#include <fstream>

#include <string>
#include <sstream>

struct Colour {
    union {
        struct {
            uint8_t r, g, b, a;
        };
        uint8_t colours[4];
    };
};

class MMU {
   private:
    uint8_t noobboy_boot_rom[0x100] = {
        0x31, 0xfe, 0xff, 0xaf, 0x21, 0xff, 0x9f, 0x32, 0xcb, 0x7c, 0x20, 0xfb, 0x21, 0x26, 0xff, 0x0e, 0x11, 0x3e,
        0x80, 0x32, 0xe2, 0x0c, 0x3e, 0xf3, 0xe2, 0x32, 0x3e, 0x77, 0x77, 0x3e, 0xfc, 0xe0, 0x47, 0x11, 0xa8, 0x00,
        0x21, 0x10, 0x80, 0x1a, 0xcd, 0x95, 0x00, 0xcd, 0x96, 0x00, 0x13, 0x7b, 0xfe, 0x34, 0x20, 0xf3, 0x11, 0xd8,
        0x00, 0x06, 0x08, 0x1a, 0x13, 0x22, 0x23, 0x05, 0x20, 0xf9, 0x3e, 0x19, 0xea, 0x10, 0x99, 0x21, 0x2f, 0x99,
        0x0e, 0x0c, 0x3d, 0x28, 0x08, 0x32, 0x0d, 0x20, 0xf9, 0x2e, 0x0f, 0x18, 0xf3, 0x67, 0x3e, 0x64, 0x57, 0xe0,
        0x42, 0x3e, 0x91, 0xe0, 0x40, 0x04, 0x1e, 0x02, 0x0e, 0x0c, 0xf0, 0x44, 0xfe, 0x90, 0x20, 0xfa, 0x0d, 0x20,
        0xf7, 0x1d, 0x20, 0xf2, 0x0e, 0x13, 0x24, 0x7c, 0x1e, 0x83, 0xfe, 0x62, 0x28, 0x06, 0x1e, 0xc1, 0xfe, 0x64,
        0x20, 0x06, 0x7b, 0xe2, 0x0c, 0x3e, 0x87, 0xe2, 0xf0, 0x42, 0x90, 0xe0, 0x42, 0x15, 0x20, 0xd2, 0x05, 0x20,
        0x4f, 0x16, 0x20, 0x18, 0xcb, 0x4f, 0x06, 0x04, 0xc5, 0xcb, 0x11, 0x17, 0xc1, 0xcb, 0x11, 0x17, 0x05, 0x20,
        0xf5, 0x22, 0x23, 0x22, 0x23, 0xc9, 0xce, 0xed, 0x66, 0x66, 0x00, 0x07, 0x00, 0x08, 0x00, 0x0f, 0x33, 0x33,
        0x00, 0x0e, 0x76, 0x67, 0xc6, 0x6c, 0x00, 0x07, 0x00, 0x09, 0x00, 0x0b, 0xdc, 0xcc, 0x6e, 0xe6, 0xcc, 0xc7,
        0xdd, 0xd8, 0x99, 0x9f, 0xbb, 0xb3, 0x33, 0x3e, 0x66, 0x67, 0x66, 0x6c, 0xcc, 0xc7, 0xdc, 0xc8, 0xbf, 0x6c,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0xa8, 0x00, 0x11, 0xa8, 0x00, 0x1a, 0x13, 0xbe, 0x20,
        0xfe, 0x23, 0x7d, 0xfe, 0x34, 0x00, 0x00, 0x06, 0x19, 0x78, 0x86, 0x23, 0x05, 0x20, 0xfb, 0x86, 0x00, 0x00,
        0x3e, 0x01, 0xe0, 0x50};

   public:
    uint8_t memory[0xFFFF];
    Cartridge *cartridge;

    uint8_t joypad = 0xFF;

    bool cgb_mode = false;

    struct clock {
        int t = 0;
        int t_instr = 0;
    } clock;

    struct timer {
        uint16_t div = 0;
        uint8_t tima = 0;
        uint8_t tma = 0;
        uint8_t tac = 0;
    } timer;

    struct Sprite {
        bool ready;
        int y;
        int x;
        uint8_t tile;
        Colour *colourPalette;
        struct {
            union {
                struct {
                    uint8_t gbcPaletteNumber1 : 1;
                    uint8_t gbcPaletteNumber2 : 1;
                    uint8_t gbcPaletteNumber3 : 1;
                    uint8_t gbcVRAMBank : 1;
                    uint8_t paletteNumber : 1;
                    uint8_t xFlip : 1;
                    uint8_t yFlip : 1;
                    uint8_t renderPriority : 1;
                };
                uint8_t value;
            };
        } options;
    } sprites[40] = {Sprite()};

    struct Tile {
        uint8_t pixels[8][8] = {0};
    } tiles[384];

    const Colour palette_colours[4] = {
        {255, 255, 255, 255},
        {192, 192, 192, 255},
        {96, 96, 96, 255},
        {0, 0, 0, 255},
    };

    Colour palette_BGP[4] = {
        {255, 255, 255, 255},
        {0, 0, 0, 255},
        {0, 0, 0, 255},
        {0, 0, 0, 255},
    };

    Colour palette_OBP0[4] = {
        {0, 0, 0, 255},
        {0, 0, 0, 255},
        {0, 0, 0, 255},
        {0, 0, 0, 255},
    };

    Colour palette_OBP1[4] = {
        {0, 0, 0, 255},
        {0, 0, 0, 255},
        {0, 0, 0, 255},
        {0, 0, 0, 255},
    };

    bool romDisabled = false;
    bool is_halted = false;
    bool trigger_halt_bug = false;

    MMU(Cartridge *cartridge);

    void load_boot_rom(std::string location);
    void load_default_boot_rom();

    void write_byte(uint16_t address, uint8_t value);
    uint8_t read_byte(uint16_t address);

    void write_short(uint16_t address, uint16_t value);
    uint16_t read_short(uint16_t address);

    void write_short_stack(uint16_t *sp, uint16_t value);
    uint16_t read_short_stack(uint16_t *sp);

    void UpdateTile(uint16_t address, uint8_t value);
    void UpdateSprite(uint16_t address, uint8_t value);
    void UpdatePalette(Colour *palette, uint8_t value);

    void save_game_state();
};
